#--------------------------------------------------------------------------------#
#THIS CODE REPLICATES ALL TEN TABLES FROM HARRIS ET AL. 2017. -----
#--------------------------------------------------------------------------------#
library(tidyverse)
library(readstata13)
library(xtable)
library(plm)
library(lfe)
library(texreg) 
library(stargazer)
library(foreign)
#--------------------------------------------------------------------------------#
#Table 1 ---
#--------------------------------------------------------------------------------#
#table 1 is aggregates.dta
#--------------------------------------------------------------------------------#
#Table 2 ---
#--------------------------------------------------------------------------------#
df <- read.dta13("raw-data/HPBM-AEJ-1033.dta") #data from Harris

my.summary = function(x, na.rm=TRUE){
  result = c(Mean=mean(x, na.rm=na.rm),
             SD=sd(x, na.rm=na.rm),
             Min=min(x, na.rm=na.rm),
             Max=max(x, na.rm=na.rm), 
             Observations=length(x))
}

# identifying numeric columns
ind = c(49:56,11:18,48,167:168,135:136,21:23,153,166,169,43:45,47,58,75:77)

# applying the function to numeric columns only
table1 = as.data.frame(sapply(df[, ind], my.summary))  
table1 = t(table1)
stargazer(table1)

#--------------------------------------------------------------------------------#
#Table 3 ----
#--------------------------------------------------------------------------------#
#need to get lags for officers killed and crime rates
df <- df %>%
  mutate_at(vars("zHUdI1","zHUdI6","zHUsIland","zHUsIhidta","SumUSitems",
                 "HAllA", "zHAd1", "zHAd6", "zHAdl", "zHAdh", "HAllB", "zHBd1",
                 "zHBd6", "zHBdl", "zHBdh", 'HAllC0', 'zHC0d1' ,'zHC0d6', 'zHC0dl',
                 'zHC0dh', 'HAllC1' ,'zHC1d1', 'zHC1d6', 'zHC1dl', 'zHC1dh', 'HAllC2',
                 'zHC2d1' ,'zHC2d6', 'zHC2dl' ,'zHC2dh'),.funs=funs(./100000)) %>%
  mutate(fips = str_pad(fips,width=2,pad=0,side="left"))

df <- df %>%
  group_by(fips) %>%
  mutate_at(vars(zHUdI1,zHUdI6,zHUsIland,zHUsIhidta,HAllA,zHAd1, zHAd6, zHAdl, zHAdh,zHUdIland,
                 HAllB,zHBd1,zHBd6,zHBdl,zHBdh,zHUdIhidta,
                 HAllC0,zHC0d1,zHC0d6,zHC0dl,zHC0dh,
                 HAllC2,zHC2d1,zHC2d6,zHC2dl,zHC2dh,killed,leoagun,logHA,logHB,logHC0,logHC2,
                 log_sum_items,SumUSitems,SumUSvalue,log_sum_value_ttl,zHUd1,zHUd6,zHUdland,zHUdhitda),
            funs(lag=dplyr::lag(.)))%>%
  select(-ti1,-ti13,-ti14)   

#save data for later running of models 
save(df,file='data/harris.RData')
write.dta(df,file='data/HPBM.dta')

table3ALL = as.formula(paste('log_sum_items ~  SumUSitems+zHUdI1+zHUdI6+zHUsIland+zHUsIhidta+
                             killed_lag + leoagun_lag + PCPI+ UnemploymentRate+Pop+econmiss+', 
                             paste(colnames(df)[c(37:47,74)], collapse='+'),
                             '| fips+year','|0|fips'))

table3ALLlm<-felm(table3ALL, data=df,exactDOF=40404)
summary(table3ALLlm)

table3WEAPONS = as.formula(paste('logHA ~ HAllA +zHAd1 +zHAd6 +zHAdl+ zHAdh+
                                 killed_lag + leoagun_lag + PCPI+ UnemploymentRate+Pop+', 
                                 paste(colnames(df)[c(37:47,74)], collapse='+'),
                                 '| fips+year','|0|fips'))

table3WEAPONSlm<-felm(table3WEAPONS, data=df,exactDOF = 40404)
summary(table3WEAPONSlm)


table3OPTICS = as.formula(paste('logHB ~ HAllB +zHBd1 +zHBd6 +zHBdl +zHBdh+
                                killed_lag + leoagun_lag + PCPI+ UnemploymentRate+Pop+', 
                                paste(colnames(df)[c(37:47,74)], collapse='+'),
                                '| fips+year','|0|fips'))

table3OPTICSlm<-felm(table3OPTICS, data=df,exactDOF = 40404)
summary(table3OPTICSlm)

table3ALLVEHICLES = as.formula(paste('logHC0 ~HAllC0 +zHC0d1 +zHC0d6+ zHC0dl+ zHC0dh+
                                     killed_lag + leoagun_lag + PCPI+ UnemploymentRate+Pop+', 
                                     paste(colnames(df)[c(37:47,74)], collapse='+'),
                                     '| fips+year','|0|fips'))

table3ALLVEHICLESlm<-felm(table3ALLVEHICLES, data=df,exactDOF = 40404)
summary(table3ALLVEHICLESlm)

table3UTILITYVEHICLES = as.formula(paste('logHC1 ~ HAllC1+ zHC1d1+ zHC1d6+ zHC1dl+ zHC1dh+
                                         killed_lag + leoagun_lag + PCPI+ UnemploymentRate+Pop+', 
                                         paste(colnames(df)[c(37:47,74)], collapse='+'),
                                         '| fips+year','|0|fips'))

table3UTILITYVEHICLESlm<-felm(table3UTILITYVEHICLES, data=df,exactDOF = 40404)
summary(table3UTILITYVEHICLESlm)

table3COMBATVEHICLES = as.formula(paste('logHC2 ~ HAllC2 + zHC2d1 + zHC2d6 + zHC2dl+zHC2dh+
                                        killed_lag + leoagun_lag + PCPI+ UnemploymentRate+Pop+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year','|0|fips'))

table3COMBATVEHICLESlm<-felm(table3COMBATVEHICLES, data=df,exactDOF = 40404)
summary(table3COMBATVEHICLESlm)

stargazer(table3ALLlm,table3WEAPONSlm,table3OPTICSlm,table3ALLVEHICLESlm,
          table3UTILITYVEHICLESlm,table3COMBATVEHICLESlm)
#--------------------------------------------------------------------------------#
#Table 4: FIXED EFFECTS -----
#--------------------------------------------------------------------------------#
FEdfmodel <- df %>%
  filter(!is.na(logcom) & !is.na(laglogSumI)) %>%
  group_by(fips) %>% 
  filter(n()>1) 

table4ALLFE = as.formula(paste('logcom~log_sum_items_lag + PCPI+ UnemploymentRate+Pop+', 
                               paste(colnames(FEdfmodel)[c(37:47,74)], collapse='+'),
                               '| fips','|0|fips'))

table4ALLFElm<-felm(table4ALLFE, data=FEdfmodel,exactDOF = 804)
summary(table4ALLFElm)

table4WEAPONSFE = as.formula(paste('logcom~laglogHA + PCPI+ UnemploymentRate+Pop+', 
                                   paste(colnames(FEdfmodel)[c(37:47,74)], collapse='+'),
                                   '| fips','|0|fips'))

table4WEAPONSFElm<-felm(table4WEAPONSFE, data=FEdfmodel,exactDOF = 804) 
summary(table4WEAPONSFElm)

table4OPTICSFE = as.formula(paste('logcom~laglogHB + PCPI+ UnemploymentRate+Pop+', 
                                  paste(colnames(FEdfmodel)[c(37:47,74)], collapse='+'),
                                  '| fips','|0|fips'))

table4OPTICSFElm<-felm(table4OPTICSFE, data=FEdfmodel,exactDOF = 804)
summary(table4OPTICSFElm)


table4VEHICLESFE = as.formula(paste('logcom~laglogHC0 + PCPI+ UnemploymentRate+Pop+', 
                                    paste(colnames(FEdfmodel)[c(37:47,74)], collapse='+'),
                                    '| fips','|0|fips'))

table4VEHICLESFElm<-felm(table4VEHICLESFE, data=FEdfmodel,exactDOF = 804)
summary(table4VEHICLESFElm)


table4COMBATVEHICLESFE = as.formula(paste('logcom~logHC2_lag + PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(FEdfmodel)[c(37:47,74)], collapse='+'),
                                          '| fips','|0|fips'))

table4COMBATVEHICLESFElm<-felm(table4COMBATVEHICLESFE, data=FEdfmodel,exactDOF = 804)
summary(table4COMBATVEHICLESFElm)

stargazer(table4ALLFElm,table4WEAPONSFElm,table4OPTICSFElm,table4VEHICLESFElm,table4COMBATVEHICLESFElm)

#--------------------------------------------------------------------------------#
#Table 4 - INSTRUMENTAL VARIABLES -----
#--------------------------------------------------------------------------------#
#first, revert back to regular variable measures (following Harris et al)
dfrestore <- df %>%
  mutate_at(vars("zHUdI1","zHUdI6","zHUsIland","zHUsIhidta","SumUSitems",
                 "HAllA", "zHAd1", "zHAd6", "zHAdl", "zHAdh", "HAllB", "zHBd1",
                 "zHBd6", "zHBdl", "zHBdh", 'HAllC0', 'zHC0d1' ,'zHC0d6', 'zHC0dl',
                 'zHC0dh', 'HAllC1' ,'zHC1d1', 'zHC1d6', 'zHC1dl', 'zHC1dh', 'HAllC2',
                 'zHC2d1' ,'zHC2d6', 'zHC2dl' ,'zHC2dh'),.funs=funs(.*100000)) %>%
  group_by(fips) %>%
  mutate_at(vars(zHUdI1,zHUdI6,zHUsIland,zHUsIhidta,HAllA,zHAd1, zHAd6, zHAdl, zHAdh,zHUdIland,
                 HAllB,zHBd1,zHBd6,zHBdl,zHBdh,zHUdIhidta,
                 HAllC0,zHC0d1,zHC0d6,zHC0dl,zHC0dh,
                 HAllC2,zHC2d1,zHC2d6,zHC2dl,zHC2dh,killed,leoagun,logHA,logHB,logHC0,logHC2,
                 log_sum_items,SumUSitems,SumUSvalue,log_sum_value_ttl,zHUd1,zHUd6,zHUdland,zHUdhitda),
            funs(lag=(dplyr::lag(., n = 1, default = NA)))) %>%
  filter(!is.na(logcom)) 

FEdfIV <- dfrestore %>% 
  group_by(fips) %>% 
  filter(n()>1)

table4ALLIV = as.formula(paste('logcom~ PCPI+ UnemploymentRate+Pop+', 
                               paste(colnames(FEdfIV)[c(49:56)], collapse='+'),
                               '| fips',
                               '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+zHUdI6_lag+
                               zHUsIland_lag+zHUsIhidta_lag)|fips'))

table4ALLIVlm<-felm(table4ALLIV, data=FEdfIV,exactDOF = 804)
summary(table4ALLIVlm)

table4WEAPONSIV = as.formula(paste('logcom~  PCPI+ UnemploymentRate+Pop+', 
                                   paste(colnames(FEdfIV)[c(37:47,74)], collapse='+'),
                                   '| fips',
                                   '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                   zHAdl_lag+zHAdh_lag)|fips'))

table4WEAPONSIVlm<-felm(table4WEAPONSIV, data=FEdfIV,exactDOF = 804)
summary(table4WEAPONSIVlm)

table4OPTICSIV = as.formula(paste('logcom~ PCPI+ UnemploymentRate+Pop+', 
                                  paste(colnames(FEdfIV)[c(37:47,74)], collapse='+'),
                                  '| fips','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                  zHBdl_lag+ zHBdh_lag)|fips'))

table4OPTICSIVlm<-felm(table4OPTICSIV, data=FEdfIV,exactDOF = 804)
summary(table4OPTICSIVlm)


table4VEHICLESIV = as.formula(paste('logcom~PCPI+ UnemploymentRate+Pop+', 
                                    paste(colnames(FEdfIV)[c(37:47,74)], collapse='+'),
                                    '| fips','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                    zHC0dl_lag+ zHC0dh_lag)|fips'))

table4VEHICLESIVlm<-felm(table4VEHICLESIV, data=FEdfIV,exactDOF = 804)
summary(table4VEHICLESIVlm)


table4COMBATVEHICLESIV = as.formula(paste('logcom~  PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(FEdfIV)[c(37:47,74)], collapse='+'),
                                          '| fips','|(logHC2_lag~HAllC2_lag+zHC2d1_lag+zHC2d6_lag+
                                          zHC2dl_lag+ zHC2dh_lag)|fips'))

table4COMBATVEHICLESIVlm<-felm(table4COMBATVEHICLESIV, data=FEdfIV,exactDOF = 804)
summary(table4COMBATVEHICLESIVlm)

stargazer(table4ALLIVlm,table4WEAPONSIVlm,table4OPTICSIVlm,table4VEHICLESIVlm,table4COMBATVEHICLESIVlm)

#--------------------------------------------------------------------------------#
#Table 5 - LOG DEATHS ------
#--------------------------------------------------------------------------------#
table5ALLIV = as.formula(paste('log_off_kill~ PCPI+ UnemploymentRate+Pop+', 
                               paste(colnames(df)[c(37:47,74)], collapse='+'),
                               '| fips+year',
                               '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+zHUdI6_lag+
                               zHUsIland_lag+zHUsIhidta_lag)|fips'))

table5ALLIVlm<-felm(table5ALLIV, data=df,exactDOF = 24864)
summary(table5ALLIVlm)

table5WEAPONSIV = as.formula(paste('log_off_kill~  PCPI+ UnemploymentRate+Pop+', 
                                   paste(colnames(df)[c(37:47,74)], collapse='+'),
                                   '| fips+year',
                                   '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                   zHAdl_lag+zHAdh_lag)|fips'))

table5WEAPONSIVlm<-felm(table5WEAPONSIV, data=df,exactDOF = 24864)
summary(table5WEAPONSIVlm)

table5OPTICSIV = as.formula(paste('log_off_kill~ PCPI+ UnemploymentRate+Pop+', 
                                  paste(colnames(df)[c(37:47,74)], collapse='+'),
                                  '| fips+year','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                  zHBdl_lag+ zHBdh_lag)|fips'))

table5OPTICSIVlm<-felm(table5OPTICSIV, data=df,exactDOF = 24864)
summary(table5OPTICSIVlm)


table5VEHICLESIV = as.formula(paste('log_off_kill~PCPI+ UnemploymentRate+Pop+', 
                                    paste(colnames(df)[c(37:47,74)], collapse='+'),
                                    '| fips+year','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                    zHC0dl_lag+ zHC0dh_lag)|fips'))

table5VEHICLESIVlm<-felm(table5VEHICLESIV, data=df,exactDOF = 24864)
summary(table5VEHICLESIVlm)


table5COMBATVEHICLESIV = as.formula(paste('log_off_kill~PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(df)[c(37:47,74)], collapse='+'),
                                          '| fips+year','|(logHC2_lag~HAllC2_lag+zHC2d1_lag+zHC2d6_lag+
                                          zHC2dl_lag+ zHC2dh_lag)|fips'))

table5COMBATVEHICLESIVlm<-felm(table5COMBATVEHICLESIV, data=df,exactDOF = 24864)
summary(table5COMBATVEHICLESIVlm) #AMG FLAG: CLOSE, BUT NOT MATCH

stargazer(table5ALLIVlm,table5WEAPONSIVlm,table5OPTICSIVlm,table5VEHICLESIVlm,table5COMBATVEHICLESIVlm)

#--------------------------------------------------------------------------------#
#Table 5 - ANY DEATHS -----
#--------------------------------------------------------------------------------#
table5ALLIVany = as.formula(paste('AOffK~ PCPI+ UnemploymentRate+Pop+', 
                                  paste(colnames(df)[c(37:47,74)], collapse='+'),
                                  '| fips+year',
                                  '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+zHUdI6_lag+
                                  zHUdIland_lag+zHUsIhidta_lag)|fips'))

table5ALLIVanylm<-felm(table5ALLIVany, data=df,exactDOF = 24864)
summary(table5ALLIVanylm)

table5WEAPONSIVany = as.formula(paste('AOffK~  PCPI+ UnemploymentRate+Pop+', 
                                      paste(colnames(df)[c(37:47,74)], collapse='+'),
                                      '| fips+year',
                                      '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                      zHAdl_lag+zHAdh_lag)|fips'))

table5WEAPONSIVanylm<-felm(table5WEAPONSIVany, data=df,exactDOF = 24864)
summary(table5WEAPONSIVanylm)

table5OPTICSIVany = as.formula(paste('AOffK~ PCPI+ UnemploymentRate+Pop+', 
                                     paste(colnames(df)[c(37:47,74)], collapse='+'),
                                     '| fips+year','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                     zHBdl_lag+ zHBdh_lag)|fips'))

table5OPTICSIVanylm<-felm(table5OPTICSIVany, data=df,exactDOF = 24864)
summary(table5OPTICSIVanylm)


table5VEHICLESIVany = as.formula(paste('AOffK~PCPI+ UnemploymentRate+Pop+', 
                                       paste(colnames(df)[c(37:47,74)], collapse='+'),
                                       '| fips+year','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                       zHC0dl_lag+ zHC0dh_lag)|fips'))

table5VEHICLESIVanylm<-felm(table5VEHICLESIVany, data=df,exactDOF = 24864)
summary(table5VEHICLESIVanylm)

table5COMBATVEHICLESIVany = as.formula(paste('AOffK~  PCPI+ UnemploymentRate+Pop+', 
                                             paste(colnames(df)[c(37:47,74)], collapse='+'),
                                             '| fips+year','|(logHC2_lag~HAllC2_lag+zHC2d1_lag+zHC2d6_lag+
                                             zHC2dl_lag+ zHC2dh_lag)|fips'))

table5COMBATVEHICLESIVanylm<-felm(table5COMBATVEHICLESIVany, 
                                  data=df,exactDOF = 24864)
summary(table5COMBATVEHICLESIVanylm)

stargazer(table5ALLIVanylm,table5WEAPONSIVanylm,table5OPTICSIVanylm,
          table5VEHICLESIVanylm,table5COMBATVEHICLESIVanylm)

#--------------------------------------------------------------------------------#
#Table 6 - PANEL A, ANY ASSAULTS ------
#--------------------------------------------------------------------------------#
table6ALLIVany = as.formula(paste('anygun~ PCPI+ UnemploymentRate+Pop+', 
                                  paste(colnames(df)[c(37:47,74)], collapse='+'),
                                  '| fips',
                                  '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                  zHUdIhidta_lag)|fips')) #zHUdI6_lag missing from this inexplicably

table6ALLIVanylm<-felm(table6ALLIVany, data=df)
summary(table6ALLIVanylm)

table6WEAPONSIVany = as.formula(paste('anygun~  PCPI+ UnemploymentRate+Pop+', 
                                      paste(colnames(df)[c(37:47,74)], collapse='+'),
                                      '| fips',
                                      '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                      zHAdl_lag+zHAdh_lag)|fips'))

table6WEAPONSIVanylm<-felm(table6WEAPONSIVany, data=df)
summary(table6WEAPONSIVanylm)

table6OPTICSIVany = as.formula(paste('anygun~ PCPI+ UnemploymentRate+Pop+', 
                                     paste(colnames(df)[c(37:47,74)], collapse='+'),
                                     '| fips','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                     zHBdl_lag+ zHBdh_lag)|fips'))

table6OPTICSIVanylm<-felm(table6OPTICSIVany, data=df)
summary(table6OPTICSIVanylm)


table6VEHICLESIVany = as.formula(paste('anygun~PCPI+ UnemploymentRate+Pop+', 
                                       paste(colnames(df)[c(37:47,74)], collapse='+'),
                                       '| fips','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                       zHC0dl_lag+ zHC0dh_lag)|fips'))

table6VEHICLESIVanylm<-felm(table6VEHICLESIVany, data=df)
summary(table6VEHICLESIVanylm)

stargazer(table6ALLIVanylm,table6WEAPONSIVanylm,table6OPTICSIVanylm,
          table6VEHICLESIVanylm)

#--------------------------------------------------------------------------------#
#Table 6 - PANEL A, LOG ASSAULTS ----
#--------------------------------------------------------------------------------#
table6ALLIVlog = as.formula(paste('logLEOassault~ PCPI+ UnemploymentRate+Pop+', 
                                  paste(colnames(df)[c(37:47,74)], collapse='+'),
                                  '| fips',
                                  '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                  zHUdIhidta_lag)|fips')) #zHUdI6_lag missing from this inexplicably

table6ALLIVloglm<-felm(table6ALLIVlog, data=df)
summary(table6ALLIVloglm)

table6WEAPONSIVlog = as.formula(paste('logLEOassault~  PCPI+ UnemploymentRate+Pop+', 
                                      paste(colnames(df)[c(37:47,74)], collapse='+'),
                                      '| fips',
                                      '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                      zHAdl_lag+zHAdh_lag)|fips'))

table6WEAPONSIVloglm<-felm(table6WEAPONSIVlog, data=df)
summary(table6WEAPONSIVloglm)

table6OPTICSIVlog = as.formula(paste('logLEOassault~ PCPI+ UnemploymentRate+Pop+', 
                                     paste(colnames(df)[c(37:47,74)], collapse='+'),
                                     '| fips','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                     zHBdl_lag+ zHBdh_lag)|fips'))

table6OPTICSIVloglm<-felm(table6OPTICSIVlog, data=df)
summary(table6OPTICSIVloglm)


table6VEHICLESIVlog = as.formula(paste('logLEOassault~PCPI+ UnemploymentRate+Pop+', 
                                       paste(colnames(df)[c(37:47,74)], collapse='+'),
                                       '| fips','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                       zHC0dl_lag+ zHC0dh_lag)|fips'))

table6VEHICLESIVloglm<-felm(table6VEHICLESIVlog, data=df)
summary(table6VEHICLESIVloglm)

stargazer(table6ALLIVloglm,table6WEAPONSIVloglm,table6OPTICSIVloglm,
          table6VEHICLESIVloglm)

#--------------------------------------------------------------------------------#
#Table 6 - PANEL B, ANY DEATHS ----
#--------------------------------------------------------------------------------#
table6ALLIVanyB = as.formula(paste('anykilled~ PCPI+ UnemploymentRate+Pop+', 
                                   paste(colnames(df)[c(37:47,74)], collapse='+'),
                                   '| fips+year',
                                   '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                   zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table6ALLIVanyBlm<-felm(table6ALLIVanyB, data=df)
summary(table6ALLIVanyBlm)

table6WEAPONSIVanyB = as.formula(paste('anykilled~  PCPI+ UnemploymentRate+Pop+', 
                                       paste(colnames(df)[c(37:47,74)], collapse='+'),
                                       '| fips+year',
                                       '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                       zHAdl_lag+zHAdh_lag)|fips'))

table6WEAPONSIVanyBlm<-felm(table6WEAPONSIVanyB, data=df)
summary(table6WEAPONSIVanyBlm)

table6OPTICSIVanyB = as.formula(paste('anykilled~ PCPI+ UnemploymentRate+Pop+', 
                                      paste(colnames(df)[c(37:47,74)], collapse='+'),
                                      '| fips+year','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                      zHBdl_lag+ zHBdh_lag)|fips'))

table6OPTICSIVanyBlm<-felm(table6OPTICSIVanyB, data=df)
summary(table6OPTICSIVanyBlm)


table6VEHICLESIVanyB = as.formula(paste('anykilled~PCPI+ UnemploymentRate+Pop+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                        zHC0dl_lag+ zHC0dh_lag)|fips'))

table6VEHICLESIVanyBlm<-felm(table6VEHICLESIVanyB, data=df)
summary(table6VEHICLESIVanyBlm)

stargazer(table6ALLIVanyBlm,table6WEAPONSIVanyBlm,table6OPTICSIVanyBlm,
          table6VEHICLESIVanyBlm)

#--------------------------------------------------------------------------------#
#Table 6 - PANEL B, LOG DEATHS -----
#--------------------------------------------------------------------------------#
table6ALLIVlogB = as.formula(paste('logLEOkilled~ PCPI+ UnemploymentRate+Pop+', 
                                   paste(colnames(df)[c(37:47,74)], collapse='+'),
                                   '| fips+year',
                                   '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                   zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table6ALLIVlogBlm<-felm(table6ALLIVlogB, data=df)
summary(table6ALLIVlogBlm)

table6WEAPONSIVlogB = as.formula(paste('logLEOkilled~  PCPI+ UnemploymentRate+Pop+', 
                                       paste(colnames(df)[c(37:47,74)], collapse='+'),
                                       '| fips+year',
                                       '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                       zHAdl_lag+zHAdh_lag)|fips'))

table6WEAPONSIVlogBlm<-felm(table6WEAPONSIVlogB, data=df)
summary(table6WEAPONSIVlogBlm)

table6OPTICSIVlogB = as.formula(paste('logLEOkilled~ PCPI+ UnemploymentRate+Pop+', 
                                      paste(colnames(df)[c(37:47,74)], collapse='+'),
                                      '| fips+year','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                      zHBdl_lag+ zHBdh_lag)|fips'))

table6OPTICSIVlogBlm<-felm(table6OPTICSIVlogB, data=df)
summary(table6OPTICSIVlogBlm)


table6VEHICLESIVlogB = as.formula(paste('logLEOkilled~PCPI+ UnemploymentRate+Pop+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                        zHC0dl_lag+ zHC0dh_lag)|fips'))

table6VEHICLESIVlogBlm<-felm(table6VEHICLESIVlogB, data=df)
summary(table6VEHICLESIVlogBlm)

stargazer(table6ALLIVlogBlm,table6WEAPONSIVlogBlm,table6OPTICSIVlogBlm,
          table6VEHICLESIVlogBlm)

#--------------------------------------------------------------------------------#
#Table 7 - LOG ITEMS -----
#--------------------------------------------------------------------------------#
table7DRUGSALESIVitems = as.formula(paste('logalldrugsale~ PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(df)[c(49:56)], collapse='+'),
                                          '| fips+year',
                                          '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                          zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table7DRUGSALESIVitemslm<-felm(table7DRUGSALESIVitems, data=df)
summary(table7DRUGSALESIVitemslm)

table7DRUGPOSSIVitems = as.formula(paste('logalldrugposs~ PCPI+ UnemploymentRate+Pop+', 
                                         paste(colnames(df)[c(49:56)], collapse='+'),
                                         '| fips+year',
                                         '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                         zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table7DRUGPOSSIVitemslm<-felm(table7DRUGPOSSIVitems, data=df)
summary(table7DRUGPOSSIVitemslm)

table7PETTYIVitems = as.formula(paste('logallsmall~ PCPI+ UnemploymentRate+Pop+', 
                                      paste(colnames(df)[c(49:56)], collapse='+'),
                                      '| fips+year',
                                      '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                      zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table7PETTYIVitemslm<-felm(table7PETTYIVitems, data=df)
summary(table7PETTYIVitemslm)

table7WEAPONSIVitems = as.formula(paste('logallweapons~ PCPI+ UnemploymentRate+Pop+', 
                                        paste(colnames(df)[c(49:56)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                        zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table7WEAPONSIVitemslm<-felm(table7WEAPONSIVitems, data=df)
summary(table7WEAPONSIVitemslm)

stargazer(table7DRUGSALESIVitemslm,table7DRUGPOSSIVitemslm,table7PETTYIVitemslm,table7WEAPONSIVitemslm)

#--------------------------------------------------------------------------------#
#Table 7 - LOG WEAPONS ------
#--------------------------------------------------------------------------------#
table7DRUGSALESIVweapons = as.formula(paste('logalldrugsale~  PCPI+ UnemploymentRate+Pop+', 
                                            paste(colnames(df)[c(49:56)], collapse='+'),
                                            '| fips+year',
                                            '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                            zHAdl_lag+zHAdh_lag)|fips'))

table7DRUGSALESIVweaponslm<-felm(table7DRUGSALESIVweapons, data=df)
summary(table7DRUGSALESIVweaponslm)

table7DRUGPOSSIVweapons = as.formula(paste('logalldrugposs~  PCPI+ UnemploymentRate+Pop+', 
                                           paste(colnames(df)[c(49:56)], collapse='+'),
                                           '| fips+year',
                                           '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                           zHAdl_lag+zHAdh_lag)|fips'))

table7DRUGPOSSIVweaponslm<-felm(table7DRUGPOSSIVweapons, data=df)
summary(table7DRUGPOSSIVweaponslm)

table7PETTYIVweapons = as.formula(paste('logallsmall~  PCPI+ UnemploymentRate+Pop+', 
                                        paste(colnames(df)[c(49:56)], collapse='+'),
                                        '| fips+year',
                                        '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                        zHAdl_lag+zHAdh_lag)|fips'))

table7PETTYIVweaponslm<-felm(table7PETTYIVweapons, data=df)
summary(table7PETTYIVweaponslm)

table7WEAPONSIVweapons = as.formula(paste('logallweapons~  PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(df)[c(49:56)], collapse='+'),
                                          '| fips+year',
                                          '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                          zHAdl_lag+zHAdh_lag)|fips'))

table7WEAPONSIVweaponslm<-felm(table7WEAPONSIVweapons, data=df)
summary(table7WEAPONSIVweaponslm)

stargazer(table7DRUGSALESIVweaponslm,table7DRUGPOSSIVweaponslm,table7PETTYIVweaponslm,
          table7WEAPONSIVweaponslm)


#--------------------------------------------------------------------------------#
#Table 7 - LOG OPTICS ------
#--------------------------------------------------------------------------------#
table7DRUGSALESIVoptics = as.formula(paste('logalldrugsale~ PCPI+ UnemploymentRate+Pop+', 
                                           paste(colnames(df)[c(49:56)], collapse='+'),
                                           '| fips+year','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                           zHBdl_lag+ zHBdh_lag)|fips'))

table7DRUGSALESIVopticslm<-felm(table7DRUGSALESIVoptics, data=df)
summary(table7DRUGSALESIVopticslm)

table7DRUGPOSSIVoptics = as.formula(paste('logalldrugposs~ PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(df)[c(49:56)], collapse='+'),
                                          '| fips+year','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                          zHBdl_lag+ zHBdh_lag)|fips'))

table7DRUGPOSSIVopticslm<-felm(table7DRUGPOSSIVoptics, data=df)
summary(table7DRUGPOSSIVopticslm)

table7PETTYIVoptics = as.formula(paste('logallsmall~ PCPI+ UnemploymentRate+Pop+', 
                                       paste(colnames(df)[c(49:56)], collapse='+'),
                                       '| fips+year','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                       zHBdl_lag+ zHBdh_lag)|fips'))

table7PETTYIVopticslm<-felm(table7PETTYIVoptics, data=df)
summary(table7PETTYIVopticslm)

table7WEAPONSIVoptics = as.formula(paste('logallweapons~ PCPI+ UnemploymentRate+Pop+', 
                                         paste(colnames(df)[c(49:56)], collapse='+'),
                                         '| fips+year','|(logHB_lag~ HAllB_lag+ zHBd1_lag+ zHBd6_lag+
                                         zHBdl_lag+ zHBdh_lag)|fips'))

table7WEAPONSIVopticslm<-felm(table7WEAPONSIVoptics, data=df)
summary(table7WEAPONSIVopticslm)

stargazer(table7DRUGSALESIVopticslm,table7DRUGPOSSIVopticslm,table7PETTYIVopticslm,table7WEAPONSIVopticslm)

#--------------------------------------------------------------------------------#
#Table 7 - LOG VEHICLES --------
#--------------------------------------------------------------------------------#
table7DRUGSALESIVvehicles = as.formula(paste('logalldrugsale~PCPI+ UnemploymentRate+Pop+', 
                                             paste(colnames(df)[c(49:56)], collapse='+'),
                                             '| fips+year','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                             zHC0dl_lag+ zHC0dh_lag)|fips'))

table7DRUGSALESIVvehicleslm<-felm(table7DRUGSALESIVvehicles, data=df)
summary(table7DRUGSALESIVvehicleslm)

table7DRUGPOSSIVvehicles = as.formula(paste('logalldrugposs~PCPI+ UnemploymentRate+Pop+', 
                                            paste(colnames(df)[c(49:56)], collapse='+'),
                                            '| fips+year','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                            zHC0dl_lag+ zHC0dh_lag)|fips'))

table7DRUGPOSSIVvehicleslm<-felm(table7DRUGPOSSIVvehicles, data=df)
summary(table7DRUGPOSSIVvehicleslm)

table7PETTYIVvehicles = as.formula(paste('logallsmall~PCPI+ UnemploymentRate+Pop+', 
                                         paste(colnames(df)[c(49:56)], collapse='+'),
                                         '| fips+year','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                         zHC0dl_lag+ zHC0dh_lag)|fips'))

table7PETTYIVvehicleslm<-felm(table7PETTYIVvehicles, data=df)
summary(table7PETTYIVvehicleslm)

table7WEAPONSIVvehicles = as.formula(paste('logallweapons~PCPI+ UnemploymentRate+Pop+', 
                                           paste(colnames(df)[c(49:56)], collapse='+'),
                                           '| fips+year','|(logHC0_lag~HAllC0_lag+ zHC0d1_lag+zHC0d6_lag+
                                           zHC0dl_lag+ zHC0dh_lag)|fips'))

table7WEAPONSIVvehicleslm<-felm(table7WEAPONSIVvehicles, data=df)
summary(table7WEAPONSIVvehicleslm)

stargazer(table7DRUGSALESIVvehicleslm,table7DRUGPOSSIVvehicleslm,table7PETTYIVvehicleslm,
          table7WEAPONSIVvehicleslm)

#--------------------------------------------------------------------------------#
#Table 8 - LOG ITEMS ----
#--------------------------------------------------------------------------------#
table8HOMICIDEIVitems = as.formula(paste('Amurders~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                         paste(colnames(df)[c(37:47,74)], collapse='+'),
                                         '| fips+year',
                                         '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                         zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table8HOMICIDEIVitemslm<-felm(table8HOMICIDEIVitems, data=df)
summary(table8HOMICIDEIVitemslm)

table8ROBBERYIVitems = as.formula(paste('Arobbery~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                        zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table8ROBBERYIVitemslm<-felm(table8ROBBERYIVitems, data=df)
summary(table8ROBBERYIVitemslm)

table8GUNIVitems = as.formula(paste('Aass_gun~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                    paste(colnames(df)[c(37:47,74)], collapse='+'),
                                    '| fips+year',
                                    '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                    zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table8GUNIVitemslm<-felm(table8GUNIVitems, data=df)
summary(table8GUNIVitemslm)

table8ASSAULTIVitems = as.formula(paste('Aassault~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                        zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table8ASSAULTIVitemslm<-felm(table8ASSAULTIVitems, data=df)
summary(table8ASSAULTIVitemslm)

table8VEHICLEIVitems = as.formula(paste('Avehicle~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                        zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table8VEHICLEIVitemslm<-felm(table8VEHICLEIVitems, data=df)
summary(table8VEHICLEIVitemslm)

stargazer(table8HOMICIDEIVitemslm,table8ROBBERYIVitemslm,table8GUNIVitemslm,table8ASSAULTIVitemslm,
          table8VEHICLEIVitemslm)

#--------------------------------------------------------------------------------#
#Table 8 - LOG VALUE -----
#--------------------------------------------------------------------------------#
table8HOMICIDEIVvalue = as.formula(paste('Amurders~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                         paste(colnames(df)[c(37:47,74)], collapse='+'),
                                         '| fips+year',
                                         '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                         zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips')) 

table8HOMICIDEIVvaluelm<-felm(table8HOMICIDEIVvalue, data=df)
summary(table8HOMICIDEIVvaluelm)

table8ROBBERYIVvalue = as.formula(paste('Arobbery~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                        zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))

table8ROBBERYIVvaluelm<-felm(table8ROBBERYIVvalue, data=df)
summary(table8ROBBERYIVvaluelm)

table8GUNIVvalue = as.formula(paste('Aass_gun~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                    paste(colnames(df)[c(37:47,74)], collapse='+'),
                                    '| fips+year',
                                    '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                    zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))

table8GUNIVvaluelm<-felm(table8GUNIVvalue, data=df)
summary(table8GUNIVvaluelm)

table8ASSAULTIVvalue = as.formula(paste('Aassault~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                        zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))

table8ASSAULTIVvaluelm<-felm(table8ASSAULTIVvalue, data=df)
summary(table8ASSAULTIVvaluelm)

table8VEHICLEIVvalue = as.formula(paste('Avehicle~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                        zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))

table8VEHICLEIVvaluelm<-felm(table8VEHICLEIVvalue, data=df)
summary(table8VEHICLEIVvaluelm)

stargazer(table8HOMICIDEIVvaluelm,table8ROBBERYIVvaluelm,table8GUNIVvaluelm,table8ASSAULTIVvaluelm,
          table8VEHICLEIVvaluelm) 

#--------------------------------------------------------------------------------#
#Table 9 - LOG ITEMS -----
#--------------------------------------------------------------------------------#
table9HOMICIDEIVitems = as.formula(paste('Cmurders~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                         paste(colnames(df)[c(49:56)], collapse='+'),
                                         '| fips+year',
                                         '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                         zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table9HOMICIDEIVitemslm<-felm(table9HOMICIDEIVitems, data=df)
summary(table9HOMICIDEIVitemslm)

table9ROBBERYIVitems = as.formula(paste('Crobbery~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(49:56)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                        zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table9ROBBERYIVitemslm<-felm(table9ROBBERYIVitems, data=df)
summary(table9ROBBERYIVitemslm)

table9GUNIVitems = as.formula(paste('Cass_gun~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                    paste(colnames(df)[c(49:56)], collapse='+'),
                                    '| fips+year',
                                    '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                    zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table9GUNIVitemslm<-felm(table9GUNIVitems, data=df)
summary(table9GUNIVitemslm)

table9ASSAULTIVitems = as.formula(paste('Cassault~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(49:56)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                        zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table9ASSAULTIVitemslm<-felm(table9ASSAULTIVitems, data=df)
summary(table9ASSAULTIVitemslm)

table9VEHICLEIVitems = as.formula(paste('Cvehicle~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(49:56)], collapse='+'),
                                        '| fips+year',
                                        '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                                        zHUdI6_lag+zHUdIhidta_lag)|fips')) 

table9VEHICLEIVitemslm<-felm(table9VEHICLEIVitems, data=df)
summary(table9VEHICLEIVitemslm)

stargazer(table9HOMICIDEIVitemslm,table9ROBBERYIVitemslm,table9GUNIVitemslm,table9ASSAULTIVitemslm,
          table9VEHICLEIVitemslm)

#--------------------------------------------------------------------------------#
#Table 9 - LOG VALUE -----
#--------------------------------------------------------------------------------#
table9HOMICIDEIVvalue = as.formula(paste('Cmurders~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                         paste(colnames(df)[c(49:56)], collapse='+'),
                                         '| fips',
                                         '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                         zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips')) 

table9HOMICIDEIVvaluelm<-felm(table9HOMICIDEIVvalue, data=df)
summary(table9HOMICIDEIVvaluelm)

table9ROBBERYIVvalue = as.formula(paste('Crobbery~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(49:56)], collapse='+'),
                                        '| fips',
                                        '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                        zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))

table9ROBBERYIVvaluelm<-felm(table9ROBBERYIVvalue, data=df)
summary(table9ROBBERYIVvaluelm)

table9GUNIVvalue = as.formula(paste('Cass_gun~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                    paste(colnames(df)[c(49:56)], collapse='+'),
                                    '| fips',
                                    '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                    zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))

table9GUNIVvaluelm<-felm(table9GUNIVvalue, data=df)
summary(table9GUNIVvaluelm)

table9ASSAULTIVvalue = as.formula(paste('Cassault~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(49:56)], collapse='+'),
                                        '| fips',
                                        '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                        zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))

table9ASSAULTIVvaluelm<-felm(table9ASSAULTIVvalue, data=df)
summary(table9ASSAULTIVvaluelm)

table9VEHICLEIVvalue = as.formula(paste('Cvehicle~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                                        paste(colnames(df)[c(49:56)], collapse='+'),
                                        '| fips',
                                        '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                                        zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))

table9VEHICLEIVvaluelm<-felm(table9VEHICLEIVvalue, data=df)
summary(table9VEHICLEIVvaluelm)

stargazer(table9HOMICIDEIVvaluelm,table9ROBBERYIVvaluelm,table9GUNIVvaluelm,table9ASSAULTIVvaluelm,
          table9VEHICLEIVvaluelm) #AMG: NO MATCH HERE

#--------------------------------------------------------------------------------#
#Table 10 - PANEL A, LOG WEAPONS ------
#--------------------------------------------------------------------------------#
table10HOMICIDEIVweapons = as.formula(paste('Amurders~  PCPI+ UnemploymentRate+Pop+', 
                                            paste(colnames(df)[c(37:47,74)], collapse='+'),
                                            '| fips+year',
                                            '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                            zHAdl_lag+zHAdh_lag)|fips'))

table10HOMICIDEIVweaponslm<-felm(table10HOMICIDEIVweapons, data=df)
summary(table10HOMICIDEIVweaponslm)

table10ROBBERYIVweapons = as.formula(paste('Arobbery~  PCPI+ UnemploymentRate+Pop+', 
                                           paste(colnames(df)[c(37:47,74)], collapse='+'),
                                           '| fips+year',
                                           '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                           zHAdl_lag+zHAdh_lag)|fips'))

table10ROBBERYIVweaponslm<-felm(table10ROBBERYIVweapons, data=df)
summary(table10ROBBERYIVweaponslm)

table10GUNIVweapons = as.formula(paste('Aass_gun~  PCPI+ UnemploymentRate+Pop+', 
                                       paste(colnames(df)[c(37:47,74)], collapse='+'),
                                       '| fips+year',
                                       '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                       zHAdl_lag+zHAdh_lag)|fips'))

table10GUNIVweaponslm<-felm(table10GUNIVweapons, data=df)
summary(table10GUNIVweaponslm)

table10ASSAULTIVweapons = as.formula(paste('Aassault~  PCPI+ UnemploymentRate+Pop+', 
                                           paste(colnames(df)[c(37:47,74)], collapse='+'),
                                           '| fips+year',
                                           '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                           zHAdl_lag+zHAdh_lag)|fips'))

table10ASSAULTIVweaponslm<-felm(table10ASSAULTIVweapons, data=df)
summary(table10ASSAULTIVweaponslm)

table10VEHICLEIVweapons = as.formula(paste('Avehicle~  PCPI+ UnemploymentRate+Pop+', 
                                           paste(colnames(df)[c(37:47,74)], collapse='+'),
                                           '| fips+year',
                                           '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                           zHAdl_lag+zHAdh_lag)|fips'))

table10VEHICLEIVweaponslm<-felm(table10VEHICLEIVweapons, data=df)
summary(table10VEHICLEIVweaponslm)

stargazer(table10HOMICIDEIVweaponslm,table10ROBBERYIVweaponslm,table10GUNIVweaponslm,
          table10ASSAULTIVweaponslm,table10VEHICLEIVweaponslm)


#--------------------------------------------------------------------------------#
#Table 10 - PANEL A, LOG OPTICS -------
#--------------------------------------------------------------------------------#
table10HOMICIDEIVoptics = as.formula(paste('Amurders~  PCPI+ UnemploymentRate+Pop+', 
                                           paste(colnames(df)[c(37:47,74)], collapse='+'),
                                           '| fips+year',
                                           '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                           zHBdl_lag+zHBdh_lag)|fips'))

table10HOMICIDEIVopticslm<-felm(table10HOMICIDEIVoptics, data=df)
summary(table10HOMICIDEIVopticslm)

table10ROBBERYIVoptics = as.formula(paste('Arobbery~  PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(df)[c(37:47,74)], collapse='+'),
                                          '| fips+year',
                                          '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                          zHBdl_lag+zHBdh_lag)|fips'))

table10ROBBERYIVopticslm<-felm(table10ROBBERYIVoptics, data=df)
summary(table10ROBBERYIVopticslm)

table10GUNIVoptics = as.formula(paste('Aass_gun~  PCPI+ UnemploymentRate+Pop+', 
                                      paste(colnames(df)[c(37:47,74)], collapse='+'),
                                      '| fips+year',
                                      '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                      zHBdl_lag+zHBdh_lag)|fips'))

table10GUNIVopticslm<-felm(table10GUNIVoptics, data=df)
summary(table10GUNIVopticslm)

table10ASSAULTIVoptics = as.formula(paste('Aassault~  PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(df)[c(37:47,74)], collapse='+'),
                                          '| fips+year',
                                          '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                          zHBdl_lag+zHBdh_lag)|fips'))

table10ASSAULTIVopticslm<-felm(table10ASSAULTIVoptics, data=df)
summary(table10ASSAULTIVopticslm)

table10VEHICLEIVoptics = as.formula(paste('Avehicle~  PCPI+ UnemploymentRate+Pop+', 
                                          paste(colnames(df)[c(37:47,74)], collapse='+'),
                                          '| fips+year',
                                          '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                          zHBdl_lag+zHBdh_lag)|fips'))

table10VEHICLEIVopticslm<-felm(table10VEHICLEIVoptics, data=df)
summary(table10VEHICLEIVopticslm)

stargazer(table10HOMICIDEIVopticslm,table10ROBBERYIVopticslm,table10GUNIVopticslm,
          table10ASSAULTIVopticslm,table10VEHICLEIVopticslm)

#--------------------------------------------------------------------------------#
#Table 10 - PANEL A, LOG VEHICLES------
#--------------------------------------------------------------------------------#
table10HOMICIDEIVvehicles = as.formula(paste('Amurders~  PCPI+ UnemploymentRate+Pop+', 
                                             paste(colnames(df)[c(37:47,74)], collapse='+'),
                                             '| fips+year',
                                             '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                             zHC0dl_lag+zHC0dh_lag)|fips'))

table10HOMICIDEIVvehicleslm<-felm(table10HOMICIDEIVvehicles, data=df)
summary(table10HOMICIDEIVvehicleslm)

table10ROBBERYIVvehicles = as.formula(paste('Arobbery~  PCPI+ UnemploymentRate+Pop+', 
                                            paste(colnames(df)[c(37:47,74)], collapse='+'),
                                            '| fips+year',
                                            '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                            zHC0dl_lag+zHC0dh_lag)|fips'))

table10ROBBERYIVvehicleslm<-felm(table10ROBBERYIVvehicles, data=df)
summary(table10ROBBERYIVvehicleslm)

table10GUNIVvehicles = as.formula(paste('Aass_gun~  PCPI+ UnemploymentRate+Pop+', 
                                        paste(colnames(df)[c(37:47,74)], collapse='+'),
                                        '| fips+year',
                                        '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                        zHC0dl_lag+zHC0dh_lag)|fips'))

table10GUNIVvehicleslm<-felm(table10GUNIVvehicles, data=df)
summary(table10GUNIVvehicleslm)

table10ASSAULTIVvehicles = as.formula(paste('Aassault~  PCPI+ UnemploymentRate+Pop+', 
                                            paste(colnames(df)[c(37:47,74)], collapse='+'),
                                            '| fips+year',
                                            '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                            zHC0dl_lag+zHC0dh_lag)|fips'))

table10ASSAULTIVvehicleslm<-felm(table10ASSAULTIVvehicles, data=df)
summary(table10ASSAULTIVvehicleslm)

table10VEHICLEIVvehicles = as.formula(paste('Avehicle~  PCPI+ UnemploymentRate+Pop+', 
                                            paste(colnames(df)[c(37:47,74)], collapse='+'),
                                            '| fips+year',
                                            '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                            zHC0dl_lag+zHC0dh_lag)|fips'))

table10VEHICLEIVvehicleslm<-felm(table10VEHICLEIVvehicles, data=df)
summary(table10VEHICLEIVvehicleslm)

stargazer(table10HOMICIDEIVvehicleslm,table10ROBBERYIVvehicleslm,table10GUNIVvehicleslm,
          table10ASSAULTIVvehicleslm,table10VEHICLEIVvehicleslm) #AMG: NO MATCH HERE

#--------------------------------------------------------------------------------#
#Table 10 - PANEL B (CASE CLOSURES), LOG WEAPONS-------
#--------------------------------------------------------------------------------#
table10PANELBHOMICIDEIVweapons = as.formula(paste('Cmurders~  PCPI+ UnemploymentRate+Pop+', 
                                                  paste(colnames(df)[c(49:56)], collapse='+'),
                                                  '| fips+year',
                                                  '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                                  zHAdl_lag+zHAdh_lag)|fips'))

table10PANELBHOMICIDEIVweaponslm<-felm(table10PANELBHOMICIDEIVweapons, data=df)
summary(table10PANELBHOMICIDEIVweaponslm)

table10PANELBROBBERYIVweapons = as.formula(paste('Crobbery~  PCPI+ UnemploymentRate+Pop+', 
                                                 paste(colnames(df)[c(49:56)], collapse='+'),
                                                 '| fips+year',
                                                 '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                                 zHAdl_lag+zHAdh_lag)|fips'))

table10PANELBROBBERYIVweaponslm<-felm(table10PANELBROBBERYIVweapons, data=df)
summary(table10PANELBROBBERYIVweaponslm)

table10PANELBGUNIVweapons = as.formula(paste('Cass_gun~  PCPI+ UnemploymentRate+Pop+', 
                                             paste(colnames(df)[c(49:56)], collapse='+'),
                                             '| fips+year',
                                             '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                             zHAdl_lag+zHAdh_lag)|fips'))

table10PANELBGUNIVweaponslm<-felm(table10PANELBGUNIVweapons, data=df)
summary(table10PANELBGUNIVweaponslm)

table10PANELBASSAULTIVweapons = as.formula(paste('Cassault~  PCPI+ UnemploymentRate+Pop+', 
                                                 paste(colnames(df)[c(49:56)], collapse='+'),
                                                 '| fips+year',
                                                 '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                                 zHAdl_lag+zHAdh_lag)|fips'))

table10PANELBASSAULTIVweaponslm<-felm(table10PANELBASSAULTIVweapons, data=df)
summary(table10PANELBASSAULTIVweaponslm)

table10PANELBVEHICLEIVweapons = as.formula(paste('Cvehicle~  PCPI+ UnemploymentRate+Pop+', 
                                                 paste(colnames(df)[c(49:56)], collapse='+'),
                                                 '| fips+year',
                                                 '|(logHA_lag ~ HAllA_lag+zHAd1_lag+zHAd6_lag+
                                                 zHAdl_lag+zHAdh_lag)|fips'))

table10PANELBVEHICLEIVweaponslm<-felm(table10PANELBVEHICLEIVweapons, data=df)
summary(table10PANELBVEHICLEIVweaponslm)

stargazer(table10PANELBHOMICIDEIVweaponslm,table10PANELBROBBERYIVweaponslm,table10PANELBGUNIVweaponslm,
          table10PANELBASSAULTIVweaponslm,table10PANELBVEHICLEIVweaponslm)


#--------------------------------------------------------------------------------#
#Table 10 - PANEL B, LOG OPTICS -----
#--------------------------------------------------------------------------------#
table10PANELBHOMICIDEIVoptics = as.formula(paste('Cmurders~  PCPI+ UnemploymentRate+Pop+', 
                                                 paste(colnames(df)[c(49:56)], collapse='+'),
                                                 '| fips+year',
                                                 '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                                 zHBdl_lag+zHBdh_lag)|fips'))

table10PANELBHOMICIDEIVopticslm<-felm(table10PANELBHOMICIDEIVoptics, data=df)
summary(table10PANELBHOMICIDEIVopticslm)

table10PANELBROBBERYIVoptics = as.formula(paste('Crobbery~  PCPI+ UnemploymentRate+Pop+', 
                                                paste(colnames(df)[c(49:56)], collapse='+'),
                                                '| fips+year',
                                                '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                                zHBdl_lag+zHBdh_lag)|fips'))

table10PANELBROBBERYIVopticslm<-felm(table10PANELBROBBERYIVoptics, data=df)
summary(table10PANELBROBBERYIVopticslm)

table10PANELBGUNIVoptics = as.formula(paste('Cass_gun~  PCPI+ UnemploymentRate+Pop+', 
                                            paste(colnames(df)[c(49:56)], collapse='+'),
                                            '| fips+year',
                                            '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                            zHBdl_lag+zHBdh_lag)|fips'))

table10PANELBGUNIVopticslm<-felm(table10PANELBGUNIVoptics, data=df)
summary(table10PANELBGUNIVopticslm)

table10PANELBASSAULTIVoptics = as.formula(paste('Cassault~  PCPI+ UnemploymentRate+Pop+', 
                                                paste(colnames(df)[c(49:56)], collapse='+'),
                                                '| fips+year',
                                                '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                                zHBdl_lag+zHBdh_lag)|fips'))

table10PANELBASSAULTIVopticslm<-felm(table10PANELBASSAULTIVoptics, data=df)
summary(table10PANELBASSAULTIVopticslm)

table10PANELBVEHICLEIVoptics = as.formula(paste('Cvehicle~  PCPI+ UnemploymentRate+Pop+', 
                                                paste(colnames(df)[c(49:56)], collapse='+'),
                                                '| fips+year',
                                                '|(logHB_lag ~ HAllB_lag+zHBd1_lag+zHBd6_lag+
                                                zHBdl_lag+zHBdh_lag)|fips'))

table10PANELBVEHICLEIVopticslm<-felm(table10PANELBVEHICLEIVoptics, data=df)
summary(table10PANELBVEHICLEIVopticslm)

stargazer(table10PANELBHOMICIDEIVopticslm,table10PANELBROBBERYIVopticslm,table10PANELBGUNIVopticslm,
          table10PANELBASSAULTIVopticslm,table10PANELBVEHICLEIVopticslm)

#--------------------------------------------------------------------------------#
#Table 10 - PANEL B, LOG VEHICLES------
#--------------------------------------------------------------------------------#
table10PANELBHOMICIDEIVvehicles = as.formula(paste('Cmurders~  PCPI+ UnemploymentRate+Pop+', 
                                                   paste(colnames(df)[c(49:56)], collapse='+'),
                                                   '| fips+year',
                                                   '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                                   zHC0dl_lag+zHC0dh_lag)|fips'))

table10PANELBHOMICIDEIVvehicleslm<-felm(table10PANELBHOMICIDEIVvehicles, data=df)
summary(table10PANELBHOMICIDEIVvehicleslm)

table10PANELBROBBERYIVvehicles = as.formula(paste('Crobbery~  PCPI+ UnemploymentRate+Pop+', 
                                                  paste(colnames(df)[c(49:56)], collapse='+'),
                                                  '| fips+year',
                                                  '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                                  zHC0dl_lag+zHC0dh_lag)|fips'))

table10PANELBROBBERYIVvehicleslm<-felm(table10PANELBROBBERYIVvehicles, data=df)
summary(table10PANELBROBBERYIVvehicleslm)

table10PANELBGUNIVvehicles = as.formula(paste('Cass_gun~  PCPI+ UnemploymentRate+Pop+', 
                                              paste(colnames(df)[c(49:56)], collapse='+'),
                                              '| fips+year',
                                              '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                              zHC0dl_lag+zHC0dh_lag)|fips'))

table10PANELBGUNIVvehicleslm<-felm(table10PANELBGUNIVvehicles, data=df)
summary(table10PANELBGUNIVvehicleslm)

table10PANELBASSAULTIVvehicles = as.formula(paste('Cassault~  PCPI+ UnemploymentRate+Pop+', 
                                                  paste(colnames(df)[c(49:56)], collapse='+'),
                                                  '| fips+year',
                                                  '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                                  zHC0dl_lag+zHC0dh_lag)|fips'))

table10PANELBASSAULTIVvehicleslm<-felm(table10PANELBASSAULTIVvehicles, data=df)
summary(table10PANELBASSAULTIVvehicleslm)

table10PANELBVEHICLEIVvehicles = as.formula(paste('Cvehicle~  PCPI+ UnemploymentRate+Pop+', 
                                                  paste(colnames(df)[c(49:56)], collapse='+'),
                                                  '| fips+year',
                                                  '|(logHC0_lag ~ HAllC0_lag+zHC0d1_lag+zHC0d6_lag+
                                                  zHC0dl_lag+zHC0dh_lag)|fips'))

table10PANELBVEHICLEIVvehicleslm<-felm(table10PANELBVEHICLEIVvehicles, data=df)
summary(table10PANELBVEHICLEIVvehicleslm)

stargazer(table10PANELBHOMICIDEIVvehicleslm,table10PANELBROBBERYIVvehicleslm,table10PANELBGUNIVvehicleslm,
          table10PANELBASSAULTIVvehicleslm,table10PANELBVEHICLEIVvehicleslm)




